package com.ddc.study.base;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.ddc.study.R;
import com.ddc.study.user.UserManager;
import com.ddc.study.util.Log;
import com.ddc.study.util.ToastUtil;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

/**
 * Created on 2018/3/28.
 *
 * @author lshun
 * @version 1.0
 */
public abstract class BaseActivity extends AppCompatActivity {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    protected final String TAG = getClass().getSimpleName();

    private MyDialogFragment mDialogFragment;

    @LayoutRes
    protected abstract int getLayoutRes();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreate -> ");
        super.onCreate(savedInstanceState);
        //强制竖屏
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (getLayoutRes() != -1) {
            setContentView(getLayoutRes());
            ButterKnife.bind(this);
        }
    }

    @Override
    protected void onRestart() {
        Log.i(TAG, "onRestart -> ");
        super.onRestart();
    }

    @Override
    protected void onStart() {
        Log.i(TAG, "onStart -> ");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "onResume -> ");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.i(TAG, "onPause -> ");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "onStop -> ");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy -> ");
        super.onDestroy();
        ((BaseApplication) getApplication()).getRefWatcher().watch(this);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        Log.i(TAG, "onRestoreInstanceState -> ");
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        Log.i(TAG, "onSaveInstanceState -> ");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        Log.i(TAG, "onConfigurationChanged -> ");
        super.onConfigurationChanged(newConfig);
    }

    public void showDialog() {
        showDialog((String) null);
    }

    public void showDialog(String msg) {
        Log.i(TAG, "showDialog -> " + msg);
        Fragment f = getSupportFragmentManager().findFragmentByTag(TAG);
        if (f instanceof MyDialogFragment) {
            ((MyDialogFragment) f).setMsg(msg);
            ((MyDialogFragment) f).showNow(getSupportFragmentManager(), TAG);
        } else {
            MyDialogFragment.getInstance(msg).show(getSupportFragmentManager(), TAG);
        }
    }

    public void dismissDialog() {
        Log.i(TAG, "dismissDialog -> ");
        mDialogFragment.dismiss();
    }

    protected void showSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null || getCurrentFocus() == null) return;
        imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
    }

    protected void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null || getCurrentFocus() == null) return;
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

    protected void showToast(@NonNull String text) {
        ToastUtil.showToast(this, text);
    }

    protected void showToast(@StringRes int resId) {
        ToastUtil.showToast(this, resId);
    }

    protected View getLoadingView() {
        return View.inflate(this, R.layout.layout_loading, null);
    }

    protected View getLoadFailedView() {
        return View.inflate(this, R.layout.layout_load_failed, null);
    }

    public boolean isLoggedIn() {
        return ((BaseApplication) getApplication()).isLoggedIn();
    }

    protected void observe(@NonNull UserManager.UserObserver observer) {
        ((BaseApplication) getApplication()).observe(observer);
    }

    protected void removeObserver(@NonNull UserManager.UserObserver observer) {
        ((BaseApplication) getApplication()).removeObserver(observer);
    }
}
